
var user = document.querySelector('.user')
var pas = document.querySelector('.pas')
var pass = document.querySelector('.pass')
var clk = document.querySelector('.clk')
var ck = document.querySelector('.ck')
var cck = document.querySelector('#cck')
var s1 = document.querySelector('.s1')
var s2 = document.querySelector('.s2')
var s3 = document.querySelector('.s3')
var s4 = document.querySelector('.s4')
var btn = document.querySelector('#btn')
console.log(btn)
console.log(user, pas, pass, clk, ck, s1)
var flag1 = 0;
var flag2 = 0;
var flag3 = 0;
var flag4 = 0;
var flag5 = 0;
// 手机验证
user.onblur = function () {
    if (/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(user.value)) {

        console.log('格式正确')
        flag1 = 1;
        console.log(flag1)

    } else {
        s1.innerHTML = "输入有误";
        flag1 = 0
        console.log(flag1)
    }
    if (flag1 == 1) {
        s1.innerHTML = "";
    }
}
// 密码验证
pas.onblur = function () {


    if (/\d{6,18}/.test(pas.value) || /[a-z]{6,18}/.test(pas.value)) {

        flag2 = 1;
        console.log(flag2)

    } else {
        s2.innerHTML = "格式不对";
        flag2 = 0
        console.log(flag2)
    }
    if (flag2 == 1) {
        s2.innerHTML = "";
    }
}
// 密码一致验证
pass.onblur = function () {

    if (pas.value == pass.value) {
        flag3 = 1
        console.log(flag3)
    } else {
        s3.innerHTML = "两次输入不一致"
        flag3 = 0
        console.log(flag3)
    }
    if (flag3 == 1) {
        s3.innerHTML = "";

        ck.innerHTML = Math.floor((Math.random() * 9000) + 1000)
    }
}
//验证码
clk.onblur = function () {
    if (clk.value == ck.innerHTML) {
        flag4 = 1
        console.log(flag4)
        s4.innerHTML = "输入一致"
    } else {

        s4.innerHTML = "输入不一致"
        flag4 = 0
        console.log(flag4)
    }
    if (flag4 == 1) {
        s4.innerHTML = ""
    }

}
console.log(cck)
cck.onclick = function () {
    if (this.checked) {
        flag5 = 1
        console.log(flag5)
    } else {
        flag5 = 0
    }
    if (flag1 == 1 & flag2 == 1 & flag3 == 1 & flag4 == 1) {
        console.log(1111)
        btn.disabled = false
    } else {
        btn.disabled = true
    }
    // btn.onclick = function () {
    //     axios.get(regApi, {
    //         params: {
    //             username: user.value,
    //             password: pas.value
    //         }
    //     }).then(res => {
    //         console.log(res)
    //         if (res.data.code == 1) {
    //             alert(res.data.msg)
    //             location.href = 'log.html'
    //             localStorage.setItem('username', res.data.data.username)
    //             console.log(JSON.parse(username))
    //         } else {
    //             alert(res.data.msg)
    //         }
    //     }).catch(error => {
    //         console.log(error)
    //     })
    // }


}

$('#btn').click(function () {
    $.get('http://47.104.244.134:8080/username.do', { username: $('.user').val() }).done(data1 => {
        if (data1.code == 1) {
            $.post('http://47.104.244.134:8080/usersave.do', { username: $('.user').val(), password: $('.pas').val(), sex: "男", email: user.value })
                .done(data => {

                    if (data.code == 0) {
                        location.href = "log.html";

                    } else if (data.code == 1) {
                        alert('data.msg')
                    }
                })
        } else if (data1.code == 0) {
            alert('用户已存在')
        }

    })

})